package com.chuantou.yuba.ui.widget;

import android.content.Context;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

public class EllipsizeTextView extends TextView {

    private View  mIndicatorView;
    private boolean mExpand = false;
    private int mMaxLines = 6;
    private boolean mIsMesure;

    public EllipsizeTextView(Context context, AttributeSet attrs, int defStyle) {
        super( context, attrs, defStyle);
        setupViews();
    }

    public EllipsizeTextView(Context context, AttributeSet attrs) {
        super( context, attrs);
        setupViews();
    }

    public EllipsizeTextView(Context context) {
        super( context);
        setupViews();
    }

    private void setupViews() {
        setEllipsize( TruncateAt.END);
    }

    public void setIndicatorView(View view) {
        this.mIndicatorView = view;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mIsMesure = true;
        if(mMaxLines != Integer.MAX_VALUE) {
            setMaxLines( Integer.MAX_VALUE);
        }
        super.onMeasure( widthMeasureSpec, heightMeasureSpec);
        int lineCount = getLineCount();
        if(!mExpand && lineCount >= 6) {
            setMaxLines( 6);
            super.onMeasure( widthMeasureSpec, heightMeasureSpec);
            if(mIndicatorView != null) {
                mIndicatorView.setVisibility( View.VISIBLE);
            }
        }else{
            if(mIndicatorView != null) {
                mIndicatorView.setVisibility( View.GONE);
            }
        }
        mIsMesure = false;
    }

    public void setExpand(boolean expand) {
        if(this.mExpand == expand)
            return;
        this.mExpand = expand;
        requestLayout();
        invalidate();

    }

    @Override
    public void requestLayout() {
        if(mIsMesure)
            return;
        super.requestLayout();
    }

    @Override
    public void setMaxLines(int maxlines) {
        this.mMaxLines = maxlines;
        super.setMaxLines( maxlines);
    }
}